package com.koron.order.defect.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseQueryBean;
import com.koron.common.core.util.CodeTools;
import com.koron.order.defect.bean.po.DefectExecute;
import com.koron.order.defect.mapper.DefectExecuteMapper;
import com.koron.order.defect.service.DefectExecuteService;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (DefectExecute)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-03 16:48:35
 */
@Service("defectExecuteService")
public class DefectExecuteServiceImpl implements DefectExecuteService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, DefectExecute defectExecute) {

        int i = factory.getMapper(DefectExecuteMapper.class).insert(defectExecute);
        return i;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<DefectExecute> defectExecuteList) {

        DefectExecuteMapper mapper = factory.getMapper(DefectExecuteMapper.class);
        mapper.deleteByOrderId(defectExecuteList.get(0).getDefectId());
        defectExecuteList.forEach(p -> {
            p.setCreateBy(getCurrUser().getAccount());
            p.setCreateByName(getCurrUser().getName());
            p.setCreateTime(new Date());
        });
        return mapper.insertBatch(defectExecuteList);

    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, DefectExecute defectExecute) {
        int i = factory.getMapper(DefectExecuteMapper.class).update(defectExecute);
        return i;
    }

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, DefectExecute defectExecute) {


        if (StringUtils.isEmpty(defectExecute.getId())) {
            defectExecute.setId(CodeTools.getCode32());
        }

        int i = factory.getMapper(DefectExecuteMapper.class).insertOrUpdate(defectExecute);

        return i;

    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        int i = factory.getMapper(DefectExecuteMapper.class).deleteById(id);
        return i;
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        int i = factory.getMapper(DefectExecuteMapper.class).deleteByOrderId(orderId);
        return i;
    }

    @Override
    public int deleteByOrderExecuteId(SessionFactory factory, String executeId) {
        return factory.getMapper(DefectExecuteMapper.class).deleteByOrderExecuteId(executeId);
    }

    @Override
    @TaskAnnotation("queryById")
    public DefectExecute queryById(SessionFactory factory, String id) {

        DefectExecute defectExecute = factory.getMapper(DefectExecuteMapper.class).queryById(id);

        return defectExecute;
    }

    @Override
    @TaskAnnotation("queryList")
    public PageInfo queryList(SessionFactory factory, DefectExecute defectExecute, BaseQueryBean pageBean) {

        PageHelper.startPage(pageBean.getPage(), pageBean.getPageSize());

        List<DefectExecute> list = factory.getMapper(DefectExecuteMapper.class).queryList(defectExecute);

        return new PageInfo(list);
    }

}